package mathematics;


/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/10/23
 * 分数类
 */
public class Fraction {
    /**
     * 分子
     */
    private int numerator;
    /**
     * 分母
     */
    private int denominator;

    public int getNumerator() {
        return numerator;
    }

    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public void setDenominator(int denominator) {
        this.denominator = denominator;
    }

    public Fraction() {
    }


    /**
     * 计算最大公约数
     *
     * @param a 分子
     * @param b 分母
     * @return 返回最大公约数
     * @author 杜伟毅
     * @since 2024/10/23 0023 17:14
     */
    private int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    public Fraction(int numerator, int denominator) {
        if (denominator == 0) {
            throw new IllegalArgumentException("分母不能为0");
        }
        //确保分子分母没有公约数
//        int gcd = gcd(numerator, denominator);
//        this.numerator = numerator / gcd;
//        this.denominator = denominator / gcd;
        //不进行约分
        this.numerator = numerator;
        this.denominator = denominator;

        // 如果分母为负数，分子和分母都取反
        if (this.denominator < 0) {
            this.numerator = -this.numerator;
            this.denominator = -this.denominator;
        }
    }

    /**
     * 比较两个分数是否相等
     *
     * @param fraction1 分数1
     * @param fraction2 分数2
     * @return 对函数返回值的描述
     * @author 杜伟毅
     * @since 2024/10/23 0023 19:57
     */
    public static boolean isSameFraction(Fraction fraction1, Fraction fraction2) {
        double valueA = (double) fraction1.getNumerator() / fraction1.getDenominator();
        double valueB = (double) fraction2.getNumerator() / fraction2.getDenominator();
        if (valueA == valueB) {
            return true;
        }
        return false;
    }


    /**
     * 比较分数大小，返回较大的分数
     *
     * @param fraction1 分数1
     * @param fraction2 分数2
     * @return 对函数返回值的描述
     * @author 杜伟毅
     * @since 2024/10/23 0023 19:32
     */
    public String findMaxFraction(Fraction fraction1, Fraction fraction2) {
        double valueA = (double) fraction1.getNumerator() / fraction1.getDenominator();
        double valueB = (double) fraction2.getNumerator() / fraction2.getDenominator();
        if (Fraction.isSameFraction(fraction1, fraction2))
            System.out.println("分数相等");

        if (valueA > valueB) {
            return fraction1.toString() + ">" + fraction2.toString();
        } else if (valueA < valueB) {
            return fraction2.toString() + " > " + fraction1.toString();
        }
        return null;
    }

    /**
     * 两个分数相加
     *
     * @param fraction1 分数1
     * @param fraction2 分数2
     * @return 对函数返回值的描述
     * @author 杜伟毅
     * @since 2024/10/23 0023 23:08
     */
    public String add(Fraction fraction1, Fraction fraction2) {
        Fraction fractionA = fraction1;
        Fraction fractionB = fraction2;
        //如果分母不相等，先通分
        if (fraction1.getDenominator() != fraction2.getDenominator()) {
            fractionA = new Fraction(fraction1.getNumerator() * fraction2.getDenominator(), fraction1.getDenominator() * fraction2.getDenominator());
            fractionB = new Fraction(fraction2.getNumerator() * fraction1.getDenominator(), fraction2.getDenominator() * fraction1.getDenominator());

        }
        // 计算分子的和
        int numeratorSum = fractionA.getNumerator() + fractionB.getNumerator();
        int denominator = fractionA.getDenominator();

        //如果分子相加等于分母，直接返回1
        if (fractionA.getNumerator() + fractionB.getNumerator() == fractionA.getDenominator()) {
            return "1";
        }
        //如果分子相加等于0，直接返回0
        if (fractionA.getNumerator() + fractionB.getNumerator() == 0) {
            return "0";
        }
        // 如果分子大于分母，转换为带分数
        if (numeratorSum > denominator) {
            // 整数部分
            int wholePart = numeratorSum / denominator;

            // 分数部分
            int fractionPart = numeratorSum % denominator;
            return wholePart + " " + fractionPart + "/" + denominator;
        }
        //如果分子相加等于分母的整数倍，直接返回整数
        if (numeratorSum % denominator == 0) {
            return Integer.toString(numeratorSum / denominator);
        }
        return numeratorSum + "/" + denominator;
    }


    /**
     * 两个分数相减
     *
     * @param fraction1 分数1
     * @param fraction2 分数2
     * @author 杜伟毅
     * @since 2024/10/23 0023 23:29
     */
    public String sub(Fraction fraction1, Fraction fraction2) {
        Fraction fractionA = fraction1;
        Fraction fractionB = fraction2;
        //如果分母不相等，先通分
        if (fraction1.getDenominator() != fraction2.getDenominator()) {
            fractionA = new Fraction(fraction1.getNumerator() * fraction2.getDenominator(), fraction1.getDenominator() * fraction2.getDenominator());
            fractionB = new Fraction(fraction2.getNumerator() * fraction1.getDenominator(), fraction2.getDenominator() * fraction1.getDenominator());

        }
        return fractionA.getNumerator() - fractionB.getNumerator() + "/" + fractionA.getDenominator();
    }

    /**
     * 整数乘分数的重载方法
     *
     * @param num      整数
     * @param fraction 分数
     * @return 对函数返回值的描述
     * @author 杜伟毅
     * @since 2024/10/24 0024 10:01
     */
    public String multiply(Integer num, Fraction fraction) {
        Fraction fraction1 = new Fraction(num * fraction.getNumerator(), fraction.getDenominator());
        int numerator = fraction1.getNumerator();
        int denominator = fraction1.getDenominator();

        //如果分子相加等于分母的整数倍，直接返回整数
        if (numerator % denominator == 0) {
            return Integer.toString(numerator / denominator);
        }

        // 如果分子大于分母，转换为带分数。
        if (numerator > denominator) {
            // 整数部分
            int wholePart = numerator / denominator;
            // 分数部分
            int fractionPart = numerator % denominator;
            return wholePart + " " + fractionPart + "/" + denominator;
        }
        return fraction1.toString();
    }

    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }

}
